#include "Area.h"
#include "Drawable.h"
#include "utf8.h"
#include "ansi.h"
#include <cstdlib>
namespace qing {
	Area::Area(Drawable *d, int w, int h, int x, int y, int rotate = 0, int fontsize=18): Drawable(w, h, x, y, rotate, fontsize) {
		this->d = d;
	}
	Color Area::_get(int x, int y) {
		return d->get_color(this->x + x, this->y + y);
	}
	void Area::_p(int x, int y, Color &clr) {
		d->point(this->x + x, this->y + y, clr);
	}
	void Area::_up(int x, int y, int w, int h, int level) {
		d->up(this->x + x, this->y + y, w, h, level);
	}
	void Area::_down(int x, int y, int w, int h, int level) {
		d->down(this->x + x, this->y + y, w, h, level);
	}
	void Area::_left(int x, int y, int w, int h, int level) {
		d->left(this->x + x, this->y + y, w, h, level);
	}
	void Area::_right(int x, int y, int w, int h, int level) {
		d->right(this->x + x, this->y + y, w, h, level);
	}
	void Area::flush(Drawable *n, bool border) {
		Rectangle size = n->get_size();
		Rectangle pos = n->get_pos();
		Color &clr = n->getFontsColor();
		if (border) {
			rectangle(pos.w-1, pos.h-1, size.w+1, size.h+1, clr);
		}
		Area *p = dynamic_cast<Area*>(n);
		if (p != nullptr) return;

		// too slow
		// 太慢了
		for (int x = 0; x < size.w; ++x) {
			for (int y = 0; y < size.h; ++y) {
				Color color = n->get_color(x, y);
				point(pos.w + x, pos.h + y, color);
			}
		}
	}
}
